#include "MenuState.h"

using namespace Ogre;

MenuState::MenuState()
{
	m_bQuit			= false;
	m_FrameEvent	= Ogre::FrameEvent();
	m_bCalcTray		= 0;
}

//////////////////////////////////////////////////////////////////////////

void MenuState::enter()
{
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering MenuState......");
	m_pSceneMgr		= OgreFramework::getSingletonPtr()->m_pRoot->
						createSceneManager(ST_GENERIC, "MenuSceneMgr");

	m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.7f, 0.7f, 0.7f));

	m_pCamera		= m_pSceneMgr->createCamera("MenuCam");
	m_pCamera->setPosition(Vector3(0, 25, 0));
	m_pCamera->lookAt(Vector3(0, 0, 0));
	m_pCamera->setNearClipDistance(1);

	m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
		Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

	OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

	OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
	OgreFramework::getSingletonPtr()->m_pTrayMgr->showFrameStats(OgreBites::TL_BOTTOMLEFT);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->showLogo(OgreBites::TL_BOTTOMRIGHT);
	
	
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "EnterBtn", "Enter Game State", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createButton(OgreBites::TL_CENTER, "ExitBtn", "Exit Game", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createLabel(OgreBites::TL_TOP, "MenuLb1", "Menu mode", 250);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->createCheckBox(OgreBites::TL_CENTER, "Mode", "Navigation Camera", 250)->setChecked(false);
	OgreFramework::getSingletonPtr()->m_pTrayMgr->showCursor();

	createScene();

}

void	MenuState::createScene()
{
	m_pCamera		= m_pSceneMgr->createCamera("MenuCamera");
	m_pCamera->setPosition(Vector3(0, 0, 200));
	m_pCamera->lookAt(Vector3(0, 0, 0));
	m_pCamera->setNearClipDistance(5);

	m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
		Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));
	OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

	Rectangle2D *rect = new Rectangle2D(true);
	rect->setCorners(-1.0f,1.0f,1.0f,-1.0f);
	rect->setMaterial("mushroom/TrippyBox"); 
	rect->setRenderQueueGroup(Ogre::RENDER_QUEUE_OVERLAY);

	AxisAlignedBox *aabb = new AxisAlignedBox();
	aabb->setInfinite();

	SceneNode* backNode=m_pSceneMgr->getRootSceneNode()->createChildSceneNode("BackgroundNode");             
	backNode->attachObject(rect);
}

//////////////////////////////////////////////////////////////////////////

void MenuState::exit()
{

	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Leaving MenuState...");
	m_pSceneMgr->destroyCamera(m_pCamera);
	if (m_pSceneMgr)
	{
		OgreFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);

		OgreFramework::getSingletonPtr()->m_pTrayMgr->clearAllTrays();
		OgreFramework::getSingletonPtr()->m_pTrayMgr->destroyAllWidgets();
		OgreFramework::getSingletonPtr()->m_pTrayMgr->setListener(0);

	}
}

bool	MenuState::keyPressed(const OIS::KeyEvent &arg)
{
	if (OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
	{
		m_bQuit	= true;
		return true;
	}

	OgreFramework::getSingletonPtr()->keyPressed(arg);
	return true;
}

//////////////////////////////////////////////////////////////////////////

bool MenuState::keyReleased(const OIS::KeyEvent &arg)
{
	OgreFramework::getSingletonPtr()->keyReleased(arg);
	return true;
}

//////////////////////////////////////////////////////////////////////////

bool MenuState::mouseMoved( const OIS::MouseEvent &arg )
{
	if (OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseMove(arg))
	{
		return true;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////

bool	MenuState::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	if (OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseDown(arg, id))
	{
		return true;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////

bool MenuState::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	if (OgreFramework::getSingletonPtr()->m_pTrayMgr->injectMouseUp(arg, id))
	{
		return true;
	}

	return true;
}

void	MenuState::update(double timeSinceLastFrame)
{
	m_FrameEvent.timeSinceLastFrame	= timeSinceLastFrame;
	
	OgreFramework::getSingletonPtr()->m_pTrayMgr->frameRenderingQueued(m_FrameEvent);
	if (m_bCalcTray < 2)
	{
		OgreFramework::getSingletonPtr()->m_pTrayMgr->adjustTrays();
		++m_bCalcTray;
	}
	if (m_bQuit == true)
	{
		shutdown();
		return;
	}

}

void MenuState::buttonHit(OgreBites::Button* button)
{
	if (button->getName() == "ExitBtn")
	{
		m_bQuit = true;
	}else if(button->getName() == "EnterBtn")
		changeAppState(findByName(("GameState")));

}

size_t						SelectMode;

void MenuState::checkBoxToggled(OgreBites::CheckBox* box)
{
	
	SelectMode = box->isChecked();
}